/*
 * Copyright 2011 evswork Team
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 *  
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */

package org.evswork.getwit;

import java.util.Timer;

import org.evswork.getwit.smackwrap.Talk;

import android.content.Context;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageButton;

public class EditPageWrapper implements OnClickListener {
	GttMainActivity act;
	ViewGroup EditLayout;
	RefTweetView RefView;
	ImageButton ClearImBtn,PostImBtn,BackImBtn;
	EditText TweetEdit;
	private Timer popupTimer;
	
	int prev_status;
	
	public static final int FROM_EDIT = 0;
	public static final int FROM_MAIN = 1;
	public static final int FROM_TWEET = 2;
	
	
	public EditPageWrapper(GttMainActivity act) {
		this.act = act;
		prev_status = FROM_EDIT;
		bindView();
	}

	private void bindView() {
		EditLayout = (ViewGroup)act.findViewById(R.id.main_edit_layout);
		ClearImBtn = (ImageButton)EditLayout.findViewById(R.id.clear_btn);
		PostImBtn = (ImageButton)EditLayout.findViewById(R.id.post_btn);
		BackImBtn = (ImageButton)EditLayout.findViewById(R.id.back_btn);
		TweetEdit = (EditText)EditLayout.findViewById(R.id.tweet_edit);
		RefView = (RefTweetView)EditLayout.findViewById(R.id.ref_tweet_view);
		
		ClearImBtn.setOnClickListener(this);
		PostImBtn.setOnClickListener(this);
		BackImBtn.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		String msg;
		switch (v.getId()) {
		case R.id.clear_btn:
			TweetEdit.setText("");
			break;
		case R.id.post_btn:
			msg = new StringBuilder(RefView.toString())
				.append(' ')
				.append(getMsg())
				.toString();
			if (msg.length()>0) {
				Talk.send(msg);
				TweetEdit.setText("");
				RefView.setVisibility(View.INVISIBLE);
			}
			switch (prev_status) {
			case FROM_EDIT:
				break;
			case FROM_MAIN:
				act.showHomeLayout();
				break;
			case FROM_TWEET:
				act.showTweetLayout();
			}
			break;
		case R.id.back_btn:
			act.showHomeLayout();
			break;
		}
	}
	
	public String getMsg() {
		return TweetEdit.getText().toString();
	}
	
	public void setMsg(String msg) {
		TweetEdit.setText(msg);
	}
	
	public void show(int status,boolean needref) {
		prev_status = status;
		EditLayout.setVisibility(View.VISIBLE);
		if (needref) {
			RefView.setVisibility(View.VISIBLE);
		} else {
			RefView.setVisibility(View.GONE);
		}
		if (PreferenceManager.getDefaultSharedPreferences(act)
				.getBoolean("softkey_popup", false)
		) {
			showSoftKeyboard();
		}
	}
	
	public void hide() {
		EditLayout.setVisibility(View.INVISIBLE);
		if (popupTimer != null)
			popupTimer.cancel();
	}
	
	private void showSoftKeyboard() {
		/*
		popupTimer = new Timer();
		popupTimer.schedule(new TimerTask() {
			@Override
			public void run() {
				try {
					TweetEdit.requestFocus();
					InputMethodManager m=(InputMethodManager) act.getSystemService(Context.INPUT_METHOD_SERVICE);
					m.toggleSoftInput(0, InputMethodManager.SHOW_FORCED);
				} catch (Exception e) {
					Log.e("Getwit",e.toString());
				}
			}	
		}, 1000L); 
		*/
		TweetEdit.requestFocus();
		InputMethodManager m=(InputMethodManager) act.getSystemService(Context.INPUT_METHOD_SERVICE);
		m.toggleSoftInput(0, InputMethodManager.SHOW_FORCED);
	}
	
}
